package org.example.maven.museumuser.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.example.maven.museumuser.annotation.Anonymous;
import org.example.maven.museumuser.domain.pojo.Result;
import org.example.maven.museumuser.domain.pojo.exhibition.ExhibitionOrder;
import org.example.maven.museumuser.service.ExhibitionService;
import org.example.maven.museumuser.utils.ThreadlocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/exhibition")
@CrossOrigin
public class exhibitionController {
    @Autowired
    private ExhibitionService exhibitionService;

    //获取展览的list数据
    @GetMapping
    @PreAuthorize("permitAll()")
    public Result getAllExhibition(){
        //System.out.println("111");
        return Result.success(exhibitionService.getAllExhibition());
    }

    @GetMapping("/{id}")
    @Anonymous //标该方法可以匿名访问
    @PreAuthorize("permitAll()")
    public Result getExhibitionById(@PathVariable("id")Integer id){
        return Result.success(exhibitionService.getExhibitionById(id));
    }

    @GetMapping("/getExhibitionByDate")
    @PreAuthorize("permitAll()")
    //根据日期获取展览
    public Result getExhibitionByDate(@RequestParam("startDate") LocalDate startDate){
        return Result.success(exhibitionService.getExhibitionByDate(startDate));
    }

    @GetMapping("/getExhibitionByName/{name}")
    @PreAuthorize("permitAll()")
    //根据名称获取展览
    public Result getExhibitionByName(@PathVariable("name") String name,@RequestParam("startDate") LocalDate startDate){
        Result result=Result.success();
        System.out.println(name+startDate);
        result.put("data",exhibitionService.getExhibitionByName(name,startDate));
        result.put("detail",exhibitionService.getExhibitionDetailByName(name,startDate));
        return result;
    }

    @PostMapping("/submitExhibitionReservation")
    @PreAuthorize("permitAll()")
    //提交展览预约
    public Result submitExhibitionReservation(@RequestBody ExhibitionOrder exhibitionOrder) throws JsonProcessingException {
        System.out.println(exhibitionOrder);
        List<String> list=new ArrayList<>();
        for(int i=0;i<exhibitionOrder.getUserOrder().size();i++){
            exhibitionOrder.getUserOrder().get(i).setCreateTime(LocalDateTime.now());
            String res=exhibitionService.submitExhibitionReservation(exhibitionOrder.getUserOrder().get(i));
            if(!res.equals("预购成功")){
                list.add(exhibitionOrder.getUserOrder().get(i).getName()+res);
            }
        }
        if(list.isEmpty()){
            ThreadlocalUtils.remove();
            return Result.success("预交成功");
        }
        ThreadlocalUtils.remove();
        return Result.error("预购失败",list);
    }



}
